#
# Makefile for full system
#

export TARGET   ?= mipsel-linux-uclibc
export TOPDIR   := $(shell pwd)
export TOOLPATH ?= $(shell cat TOOLPATH)
export PATH := $(TOOLPATH):$(PATH)

export LC_ALL := C

#

ifneq ($(wildcard ROM_ID),)
export ROM_ID := `cat $(TOPDIR)/ROM_ID`
endif

TOOLS_LIST := tools
KERNEL_LIST := kernel_build kernel_install
ROOTFS_LIST := busybox rootfs
PART_LIST := bootloader $(TOOLS_LIST) $(KERNEL_LIST) $(ROOTFS_LIST) show_o3g_version
CLEAN_LIST := $(TOOLS_LIST) linux $(ROOTFS_LIST)
	
.PHONY: all toolchain $(PART_LIST)

all:  check_project $(PART_LIST) 

tools rootfs busybox:
	@make -s -C $@ all

bootloader:
	@make -s -C bootloader

kernel_install:
	@make -s -C linux install

kernel_build:
	@TARGET=mipsel-linux make -C linux all

#

#make a whole image
linuxall: 
	@make clean 
	@make -s $(PART_LIST)

#make a kernel.bin
linux: kernel_build kernel_install glue

#quickly make a filesystem
glue:
	@make -s -C rootfs glue

#make filesystem
rootfs: $(ROOTFS_LIST)

help:
	@echo "Make Command List"
	@echo "make: make all of the project without cleaning"
	@echo "make linuxall: make a whole new world"
	@echo "make linux: make kernel"
	@echo "make rootfs: make filesystem (include busybox, rootfs)"
	@echo "make busybox: make busybox and install to busybox/root"
	@echo "make glue: quickly make filesystem"
	@echo "make clean: clean linux ,filesystem"
	
show_o3g_version:
	@if [ -f rootfs/glue/get_o3g_version ]; then \
		echo "----------------------------"; \
		rootfs/glue/get_o3g_version ; \
		echo "----------------------------"; \
	fi
	
check_project:
	@if [ ! -f PROJECT ] ; then \
		make -C project_config menuconfig; \
	else \
		make -C project_config check; \
	fi

menuconfig:
	@make -C project_config clean
	@make -C project_config menuconfig
	@make -s clean



CLEAN_LIST := $(patsubst %,clean-%,$(CLEAN_LIST))

.PHONY: clean $(CLEAN_LIST)

clean: $(CLEAN_LIST)
	rm -f image/*.bin
	rm -f image/zImage.lzma
	rm -f image/squashfs.o

$(CLEAN_LIST):
	@echo "*** Cleaning $(patsubst clean-%,%,$@)..."
	@make -s -C $(patsubst clean-%,%,$@) clean

#

ifneq ($(wildcard makefile.corner),)
include makefile.corner
endif
